# 主要目标
# 1. 找出所有涨停的记录
# 2. 分析其后的第1天，第2天，。。。，第7天的单日涨幅
# 3. 分析其后的第1天，第2天，。。。，第7天的累计涨幅

import os, sys
from datetime import datetime, timedelta
import pandas as pd
from utils.common import *

stockinfo_path = 'all_stock_info_2025-01-15.csv'
df_codes = pd.read_csv(stockinfo_path)

res = []
counter = 0
for index, row in df_codes.iterrows():
    #print('.', end='', flush=True)
    counter += 1
    if counter % 100 == 0:
        print('.', end='', flush=True)
    #sys.stdout.buffer.write('.')
    dr = row.tolist()
    stock_code = dr[1]
    stock_file = f'./data/{stock_code}.csv'
    if not os.path.exists(stock_file):
        continue
    #print(stock_file)
    df = pd.read_csv(stock_file, index_col=0)
    v1 = df.high.max()
    row0 = df[df.high == v1].iloc[0]
    h1 = df[df.date == '2025-01-15'].high #.iloc[0]
    if len(h1) > 0:
        v2 = h1.iloc[0]
        p = v2/v1
        if 0.5 < p and p < 0.75:
            #print(f'{stock_code}  {row0.date}:{v1:8.2f}\t{v2:6.2f} {v2/v1:8.0%}')
            res.append((stock_code, row0.date, v1, v2, v2/v1))
# code, date, v1, v2, percent
#       sz.301030  2024-11-01   40.00   23.55  58.88%
print('\n  code        date      v1       v2  percent')
df = pd.DataFrame(res, columns=['code', 'date', 'v1', 'v2', 'percent'])
for id, row in df.sort_values(by='percent').iterrows():
    dr = row.tolist()
    if 'sz.3' in dr[0] or 'sh.68' in dr[0] or dr[2] > 20:
        continue
    print(f'{dr[0]}  {dr[1]}  {dr[2]:6.2f}  {dr[3]:6.2f}  {dr[4]:6.2%}')
df.to_csv(f'result_{datetime.now():%Y%m%d_%H%M%S}.csv')
#print(df.sort_values(by='percent').head(100))

